package com.chuangshu.chuangshuteam.controller;


import com.chuangshu.chuangshuteam.annotation.RequiresRoles;
import com.chuangshu.chuangshuteam.dto.RecruitmentCyclesDTO;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.BusinessKey;
import com.chuangshu.chuangshuteam.service.RecruitmentCyclesService;
import com.chuangshu.chuangshuteam.vo.RecruitmentCyclesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/recruitment-cycles")
public class RecruitmentCyclesController {
     @Autowired
     RecruitmentCyclesService recruitmentCyclesService;
     @RequiresRoles("member")
    @PostMapping("/add/info")
    public Result addInfo(@RequestBody RecruitmentCyclesVO recruitmentCyclesVO){
        log.info("用户添加招新公告信息 {}", recruitmentCyclesVO);
        if (recruitmentCyclesVO.getEndDate().isBefore(LocalDateTime.now())){
            return Result.fail("结束时间不能早于当前时间");
        }
        if (recruitmentCyclesVO.getStartDate().isAfter(recruitmentCyclesVO.getEndDate())){
            return Result.fail("开始时间不能晚于结束时间");
        }
        if (recruitmentCyclesVO.getTitle().isEmpty()||recruitmentCyclesVO.getDescription().isEmpty()){
            return Result.fail("请填写标题和内容");
        }
        List<String> directions = recruitmentCyclesVO.getDirections();
        for (String direction : directions) {
            if (!BusinessKey.isValidDirection(direction)) {
                return Result.fail("请填写正确的方向");
            }
        }
        recruitmentCyclesService.addInfo(recruitmentCyclesVO);

        return Result.ok("添加成功");

    }
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("获取所有招新信息");
        Result byPage = recruitmentCyclesService.getByPage(page, pageSize);

        return byPage;
    }

    @GetMapping("/query/{id}")
    public Result getById(@PathVariable Long id){
        log.info("用户根据id查询招新公告 {}",id);

        if (id==null||id<=0){
            return Result.fail("请输入正确的id");
        }
        RecruitmentCyclesDTO recruitmentCyclesDTO = recruitmentCyclesService.queryById(id);

        return Result.ok(recruitmentCyclesDTO);
    }
    @RequiresRoles("member")
    @PutMapping("/update")
public Result updateInfo(@RequestBody RecruitmentCyclesVO recruitmentCyclesVO) {
    log.info("用户更新招新公告信息 {}", recruitmentCyclesVO);

    if (recruitmentCyclesVO.getId() == null || recruitmentCyclesVO.getId() <= 0) {
        return Result.fail("请输入正确的ID");
    }

    if (recruitmentCyclesVO.getTitle().isEmpty() || recruitmentCyclesVO.getDescription().isEmpty()) {
        return Result.fail("请填写标题和内容");
    }

    recruitmentCyclesService.updateInfo(recruitmentCyclesVO);
    return Result.ok("更新成功");
}
@RequiresRoles("member")
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Long id) {
    log.info("用户删除招新公告 ID: {}", id);

    if (id == null || id <= 0) {
        return Result.fail("请输入正确的ID");
    }

    recruitmentCyclesService.deleteById(id);
    return Result.ok("删除成功");
}




}

